/// Pack, Card, and Card Repository ///

using System;
using System.Xml;
using System.IO;
using System.Collections;

namespace Xyne
{
	class Effect
	{
		private string id;
		private string proc;
		private string enemy;
		private string shortText;
		private string longText;
		private string meta;
		
		public string ID
		{
			set { id = value; }
			get { return id; }
		}
		
		public string ProcRate
		{
			set { proc = value; }
			get { return proc; }
		}
		
		public string Enemy
		{
			set { enemy = value; }
			get { return enemy; }
		}
		
		public string ShortText
		{
			set { shortText = value; }
			get { return shortText; }
		}
		
		public string LongText
		{
			set { longText = value; }
			get { return longText; }
		}
		
		public string Meta
		{
			set { meta = value; }
			get { return meta; }
		}
	}
	
	class Card
	{
		private string name;
		private string description;
		private string pattern;
		private ArrayList effects = new ArrayList();
		
		public ArrayList Effects
		{
			get { return effects; }
		}
		
		public string Name
		{
			set { name = value; }
			get { return name; }
		}
		
		public string Description
		{
			set { description = value; }
			get { return description; }
		}
		
		public string Pattern
		{
			set { pattern = value; }
			get { return pattern; }
		}
	}
	
	class Pack
	{
		private string name;
		private string description;
		private string version;
		private ArrayList cards = new ArrayList();
		
		public string Name
		{
			set { name = value; }
			get { return name; }
		}
		
		public string Description
		{
			set { description = value; }
			get { return description; }
		}
		
		public string Version
		{
			set { version = value; }
			get { return version; }
		}
		
		public ArrayList Cards
		{
			get { return cards; }
		}
	}
	
	class CardRepository
	{
		private const string PACK_FOLDER = "packs";
		private const string ROOT_NAME = "Pack";
		private const string PACK_NAME = "Name";
		private const string PACK_DESCRIPTION = "Description";
		private const string PACK_VERSION = "Version";
		private const string CARD_NODE = "Card";
		private const string CARD_NAME = "Name";
		private const string CARD_DESCRIPTION = "Description";
		private const string CARD_PATTERN = "Pattern";
		private const string EFFECT_NODE = "Effect";
		private const string EFFECT_ID = "ID";
		private const string EFFECT_CATEGORY = "Category";
		private const string EFFECT_PROC = "Proc";
		private const string EFFECT_ENEMY = "Enemy";
		private const string EFFECT_SHORTTEXT = "ShortText";
		private const string EFFECT_LONGTEXT = "LongText";
		private const string EFFECT_META = "Meta";
		
		private static CardRepository instance = new CardRepository();
		private ArrayList packs = new ArrayList();
		
		public static CardRepository GetRepository() { return instance; }
		
		public ArrayList Packs
		{
			get { return packs; }
		}
		
		private CardRepository()
		{
		}
		
		public bool LoadCards()
		{
			try
			{
				DirectoryInfo dir = Directory.CreateDirectory(PACK_FOLDER);
				
				for( int i = 0; i < dir.GetFiles().Length; i++ )
					LoadFile(dir.GetFiles().GetValue(i).ToString());
				
				return true;
			}
			catch( Exception ) {}
					
			return false;
		}
		
		private void LoadFile(string filename)
		{
			try
			{
				XmlDocument document = new XmlDocument();
				document.Load(filename);
				XmlNode root = null;
				
				for( int i = 0; i < document.ChildNodes.Count; i++ )
				{
					if( document.ChildNodes.Item(i).LocalName == ROOT_NAME )
					{
						root = document.ChildNodes.Item(1);
					}
				}
				
				if( root != null )
				{
					Pack pack = new Pack();
					
					for( int i = 0; i < root.Attributes.Count; i++ )
					{
						if( root.Attributes.Item(i).LocalName == PACK_NAME )
							pack.Name = root.Attributes.Item(i).Value;
						if( root.Attributes.Item(i).LocalName == PACK_DESCRIPTION )
							pack.Description = root.Attributes.Item(i).Value;
						if( root.Attributes.Item(i).LocalName == PACK_VERSION )
							pack.Version = root.Attributes.Item(i).Value;
					}
					
					foreach( XmlNode node in root.ChildNodes )
					{
						if( node.LocalName == CARD_NODE )
						{
							Card card = new Card();
							
							foreach( XmlNode attribute in node.Attributes )
							{
								if( attribute.LocalName == CARD_NAME )
									card.Name = attribute.Value;
								if( attribute.LocalName == CARD_DESCRIPTION )
									card.Description = attribute.Value;
								if( attribute.LocalName == CARD_PATTERN )
									card.Pattern = attribute.Value;
							}
							
							foreach( XmlNode effectNode in node.ChildNodes )
							{
								if( effectNode.LocalName == EFFECT_NODE )
								{
									Effect effect = new Effect();
									foreach( XmlNode attribute in effectNode.Attributes )
									{
										if( attribute.LocalName == EFFECT_ID )
											effect.ID = attribute.Value;
										if( attribute.LocalName == EFFECT_PROC )
											effect.ProcRate = attribute.Value;
										if( attribute.LocalName == EFFECT_ENEMY )
											effect.Enemy = attribute.Value;
										if( attribute.LocalName == EFFECT_SHORTTEXT )
											effect.ShortText = attribute.Value;
										if( attribute.LocalName == EFFECT_LONGTEXT )
											effect.LongText = attribute.Value;
										if( attribute.LocalName == EFFECT_META )
											effect.Meta = attribute.Value;
									}
									
									card.Effects.Add(effect);
								}
							}
							
							pack.Cards.Add(card);
						}
					}
					
					packs.Add(pack);
				}
			}
			catch( Exception ) {}
		}
	}
}